################################################################################
# Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH  #
#                                                                              #
#              This software is distributed under the terms of the             #
#              GNU Lesser General Public Licence (LGPL) version 3,             #
#                  copied verbatim in the file "LICENSE"                       #
################################################################################

function(fairroot_summary_header)
  set(version_str "${PROJECT_VERSION}")
  if(PROJECT_GIT_VERSION)
    string(APPEND version_str " (${PROJECT_GIT_VERSION})")
  endif()
  # message(STATUS "  ")
  # message(STATUS "               C O N F I G U R A T I O N   S U M M A R Y")
  message(VERBOSE "  ")
  message(VERBOSE "  ${Cyan}PROJECT NAME${CR}         ${BGreen}${PROJECT_NAME}${CR}")
  message(VERBOSE "  ${Cyan}VERSION${CR}              ${BGreen}${version_str}${CR}")
endfunction()

function(fairroot_summary_install_prefix)
  message(STATUS "  ")
  message(STATUS "  ${Cyan}INSTALL PREFIX${CR}       ${BGreen}${CMAKE_INSTALL_PREFIX}${CR} (change with ${BMagenta}-DCMAKE_INSTALL_PREFIX=...${CR})")
  foreach(dirtype IN ITEMS "BINDIR" "LIBDIR" "DATADIR" "INCLUDEDIR")
    if(DEFINED PROJECT_INSTALL_${dirtype})
      set(dirval "${PROJECT_INSTALL_${dirtype}}")
    elseif(DEFINED CMAKE_INSTALL_${dirtype})
      set(dirval "${CMAKE_INSTALL_${dirtype}}")
    else()
      set(dirval "")
    endif()
    if(dirval)
      fair_pad("${dirtype}" 12 " " type_padded)
      message(VERBOSE "          ${type_padded} ${dirval}")
    endif()
  endforeach()
endfunction()


function(fairroot_summary_package_dependencies)
  if(PROJECT_PACKAGE_DEPENDENCIES)
    message(STATUS "  ")
    message(STATUS "  ${Cyan}DEPENDENCY FOUND     VERSION                   PREFIX${CR}")
    foreach(dep IN LISTS PROJECT_PACKAGE_DEPENDENCIES)
      if(${dep}_VERSION)
        if(${dep} STREQUAL DDS)
          if(DDS_VERSION)
            set(version_str "${BGreen}${${dep}_VERSION}${CR}")
          else()
            set(version_str "${BGreen}${${dep}_MAJOR_VERSION}.${${dep}_MINOR_VERSION}${CR}")
          endif()
        elseif(${dep} STREQUAL Boost)
          if(Boost_VERSION_MAJOR)
            set(version_str "${BGreen}${${dep}_VERSION_MAJOR}.${${dep}_VERSION_MINOR}${CR}")
          else()
            set(version_str "${BGreen}${${dep}_MAJOR_VERSION}.${${dep}_MINOR_VERSION}${CR}")
          endif()
        else()
          set(version_str "${BGreen}${${dep}_VERSION}${CR}")
        endif()
      else()
        set(version_str "${BYellow}unknown${CR}")
      endif()
      if(PROJECT_${dep}_VERSION)
        set(version_req_str " (>= ${PROJECT_${dep}_VERSION})")
      endif()
      fair_pad(${dep} 21 " " dep_padded)
      fair_pad("${version_str}${version_req_str}" 26 " " version_padded COLOR)
      set(prefix ${${dep}_PREFIX})
      if(${dep} STREQUAL Boost)
        if(TARGET Boost::headers)
          get_target_property(boost_include Boost::headers INTERFACE_INCLUDE_DIRECTORIES)
        else()
          get_target_property(boost_include Boost::boost INTERFACE_INCLUDE_DIRECTORIES)
        endif()
        get_filename_component(prefix ${boost_include}/.. ABSOLUTE)
      elseif(${dep} STREQUAL Protobuf)
        get_filename_component(prefix ${Protobuf_INCLUDE_DIRS}/.. ABSOLUTE)
      elseif(${dep} STREQUAL Pythia6)
        get_filename_component(prefix ${PYTHIA6_LIBRARY_DIR}/.. ABSOLUTE)
      elseif(${dep} STREQUAL FairLogger)
        if(FairLogger_PREFIX)
          set(prefix ${FairLogger_PREFIX})
        else()
          set(prefix ${FairLogger_ROOT})
        endif()
      elseif(${dep} STREQUAL FairMQ)
        if(FairMQ_PREFIX)
          set(prefix ${FairMQ_PREFIX})
        else()
          set(prefix ${FairMQ_ROOT})
        endif()
      elseif(${dep} STREQUAL yaml-cpp)
        get_filename_component(prefix ${YAML_CPP_INCLUDE_DIR}/.. ABSOLUTE)
      elseif(${dep} STREQUAL Geant4VMC)
        string(REPLACE ":" ";" geant4vmc_include ${Geant4VMC_INCLUDE_DIRS})
        list(GET geant4vmc_include 0 geant4vmc_include)
        get_filename_component(prefix ${geant4vmc_include}/../.. ABSOLUTE)
      elseif(${dep} STREQUAL Geant3)
        get_target_property(geant3_include geant321 INTERFACE_INCLUDE_DIRECTORIES)
        string(REPLACE ":" ";" geant3_include ${geant3_include})
        list(GET geant3_include 0 geant3_include)
        get_filename_component(prefix ${geant3_include}/../.. ABSOLUTE)
      elseif(${dep} STREQUAL Geant4)
        list(GET Geant4_INCLUDE_DIRS 0 geant4_include)
        get_filename_component(prefix ${geant4_include}/../.. ABSOLUTE)
      elseif(${dep} STREQUAL VGM)
        string(REPLACE ":" ";" vgm_include ${VGM_INCLUDE_DIRS})
        list(GET vgm_include 0 vgm_include)
        get_filename_component(prefix ${vgm_include}/.. ABSOLUTE)
      elseif(${dep} STREQUAL ROOT)
        set(prefix ${ROOT_INSTALL_DIR})
      elseif(${dep} STREQUAL IWYU)
        get_filename_component(prefix ${IWYU_BINARY}/.. ABSOLUTE)
      elseif(${dep} STREQUAL yaml-cpp)
        get_filename_component(prefix ${YAML_CPP_INCLUDE_DIR}/.. ABSOLUTE)
      elseif(${dep} STREQUAL fmt)
        get_target_property(fmt_include fmt::fmt INTERFACE_INCLUDE_DIRECTORIES)
        get_filename_component(prefix ${fmt_include}/.. ABSOLUTE)
      elseif(${dep} STREQUAL VMC)
        get_target_property(vmc_include VMCLibrary INTERFACE_INCLUDE_DIRECTORIES)
        get_filename_component(prefix ${vmc_include}/.. ABSOLUTE)
      elseif(${dep} STREQUAL Flatbuffers)
        if(TARGET flatbuffers::flatbuffers)
          get_target_property(flatbuffers_include flatbuffers::flatbuffers INTERFACE_INCLUDE_DIRECTORIES)
        else()
          get_target_property(flatbuffers_include flatbuffers::flatbuffers_shared INTERFACE_INCLUDE_DIRECTORIES)
        endif()
        get_filename_component(prefix ${flatbuffers_include}/.. ABSOLUTE)
      elseif(${dep} STREQUAL DDS)
        set(prefix ${DDS_INSTALL_PREFIX})
      endif()

      message(STATUS "  ${BWhite}${dep_padded}${CR}${version_padded}${prefix}")

      if(${dep} STREQUAL Geant3)
        message(STATUS "                                          G3SYS: ${Geant3_SYSTEM_DIR}")
      endif()
      if(${dep} STREQUAL Geant4)
        foreach(dataset IN LISTS Geant4_DATASETS)
          fair_pad(${Geant4_DATASET_${dataset}_ENVVAR} 21 " " envvar_padded LEFT)
          message(STATUS "                          ${envvar_padded}: ${Geant4_DATASET_${dataset}_PATH}")
        endforeach()
      endif()

      unset(version_str)
      unset(version_padded)
      unset(version_req_str)
    endforeach()
  endif()
endfunction()


function(fairroot_summary_components)
  message(STATUS "  ")

  message(STATUS "  ${Cyan}COMPONENT           BUILT?  INFO${CR}")
  if(BUILD_EXAMPLES)
    set(examples_summary "${BGreen}YES${CR}    (default, disable with ${BMagenta}-DBUILD_EXAMPLES=OFF${CR})")
  else()
    set(examples_summary "${BRed} NO${CR}    (enable with ${BMagenta}-DBBUILD_EXAMPLES=ON${CR})")
  endif()
  message(STATUS "  ${BWhite}Examples${CR}             ${examples_summary}")

  if(BUILD_DOXYGEN)
    set(doxygen_summary "${BGreen}YES${CR}     (disable with ${BMagenta}-DBUILD_DOXYGEN=OFF${CR})")
  else()
    set(doxygen_summary   "${BRed} NO${CR}     (default, enable with ${BMagenta}-DBUILD_DOXYGEN=ON${CR})")
  endif()
  message(STATUS "  ${BWhite}Doxygen${CR}             ${doxygen_summary}")
endfunction()
